package com.example.administrator.newjiajiesong.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.example.administrator.newjiajiesong.R;
import com.example.administrator.newjiajiesong.widget.TitleBar;


/**
 * 日期：2017.01.03
 * <p>
 * 作者：xudiwei
 * <p>
 * 描述：Baseactivity添加相关的方法
 *
 *
 */
public class BaseActivity extends AppCompatActivity {

    private ProgressDialog mProgressDialog;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }





    public void toast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    public void toast(@StringRes int ids) {
        this.toast(getString(ids));
    }

    /**
     * 线程安全的toast
     *
     * @param msg
     */
    public void toastThreadSafe(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                toast(msg);
            }
        });

    }

    /**
     * 线程安全的toast
     *
     * @param ids
     */
    public void toastThreadSafe(@StringRes final int ids) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                toast(ids);
            }
        });

    }

    /**
     * 开启一个Activity
     *
     * @param clz
     */
    public void startActivity(Class<? extends Activity> clz) {
        startActivity(new Intent(this, clz));
    }

    /**
     * 开启一个Activity并关闭当前Activity
     *
     * @param clz
     */
    public void startActivityAndFinishSelf(Class<? extends Activity> clz) {
        startActivity(clz);
        finish();
    }

    /**
     * 初始化进度对话框
     */
    protected void showProgressDialog(String dialogMessage) {
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.setMessage(dialogMessage);
        mProgressDialog.show();
    }

    /**
     * 关闭进度对话框
     */
    protected void dismissProgressDialog() {
        if (null != mProgressDialog && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    /**
     * 初始化刷新控件的参数
     *
     * @param ids 刷新控件的id
     */
    protected SwipeRefreshLayout setSwipeRefreshLayout(@IdRes int ids) {
        SwipeRefreshLayout mRefreshLayout = (SwipeRefreshLayout) findViewById(ids);
        mRefreshLayout.setProgressViewOffset(false, 0,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics()));
        mRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
        return mRefreshLayout;
    }

    /**
     * 设置标题栏参数
     *
     * @param titleIds              标题栏的id
     * @param title                 标题
     * @param goneLeftBtn           左边按钮是否显示
     * @param goneRightBtn          右边按钮是否显示
     * @param titleBarClickListener 左右边的按钮点击事件回调接口
     * @return 返回设置好的titlebar
     */
    public TitleBar setTitleBar(@IdRes int titleIds, String title
            , boolean goneLeftBtn, boolean goneRightBtn, TitleBar.OnTitleBarClickListener titleBarClickListener) {
        TitleBar titleBar = (TitleBar) findViewById(titleIds);
        titleBar.setTitle(title);
        titleBar.visibleIvLeftBtn(goneLeftBtn);
        titleBar.visibleIvRightBtn(goneRightBtn);
        titleBar.setOnTitleBarClickListener(titleBarClickListener);
        return titleBar;
    }

    /**
     * 设置标题栏参数
     *
     * @param titleIds                 标题栏的id
     * @param title                    标题
     * @param visibleLeftBtn           左边按钮是否显示
     * @param visibleRightBtn          右边按钮是否显示
     * @param visibleRightTextBtn      右边文本按钮是否显示
     * @param titleBarAllClickListener 左右边的按钮 右边文本按钮 点击事件回调接口
     * @return 返回设置好的titlebar
     */
    public TitleBar setTitleBar(@IdRes int titleIds, String title
            , boolean visibleLeftBtn, boolean visibleRightBtn, boolean visibleRightTextBtn,
                                TitleBar.OnTitleBarAllClickListener titleBarAllClickListener) {
        TitleBar titleBar = (TitleBar) findViewById(titleIds);
        titleBar.setTitle(title);
        titleBar.visibleIvLeftBtn(visibleLeftBtn);
        titleBar.visibleIvRightBtn(visibleRightBtn);
        titleBar.visibleTvRightTextBtn(visibleRightTextBtn);
        titleBar.setOnTitleBarAllClickListener(titleBarAllClickListener);
        return titleBar;
    }

    /**
     * 显示或隐藏软键盘，如果当时是显示则隐藏，如果当前是隐藏则显示
     */
    public void showOrHide() {
        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        // 得到InputMethodManager的实例
        if (imm.isActive()) {
            // 如果开启
            imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
                    InputMethodManager.HIDE_NOT_ALWAYS);

        }
    }


    /**
     * 隐藏软键盘
     */
    public void hintKb() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        /**点击空白处隐藏键盘*/
        if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }

    }

    /**
     * 显示软键盘
     *
     * @param view
     */
    public void showKb(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, InputMethodManager.RESULT_SHOWN);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }


    /**
     * 默认的标题栏点击事件，只对返回键响应做处理
     */
    protected final TitleBar.OnTitleBarClickListener mDefaultTitleBarClickListener = new TitleBar.OnTitleBarClickListener() {
        @Override
        public void onLeftButtonClick(View view) {
            hintKb();
            finish();

        }

        @Override
        public void onRightButtonClick(View view) {

        }
    };




    /**
     * 有权限
     */
    protected void requestPermissionsSuccess() {

    }

    /**
     * 无权限
     */
    protected void requestPermissionsFailed() {

    }

    /**
     * 子类复写此方法返回需要申请的权限
     *
     * @return
     */
    protected String[] returnPermissionArr() {
        return new String[]{};
    }


}
